package cn.w.outdoor.lib.model;

import cn.w.outdoor.lib.common.CommonMethods;

public class Trans_Package {

    public final static int vType_Audio = 0;
    public final static int vType_Video = 1;

    private static int audio_package_id = 0;
    private static int video_package_id = 0;

    public static void resetAudioPackageId() {
        audio_package_id = 0;
    }

    public static void resetVideoPackageId() {
        video_package_id = 0;
    }

    private static int getAudioPackageId() {
        return ++audio_package_id;
    }

    private static int getVideoPackageId() {
        return ++video_package_id;
    }

    private int vType;
//    private int packageId;
//    private int lostType;
//    private int len;
//    private byte[] data;

    private byte[] trans_package;

    /**
     * 实际数据内容初始化
     */
    public Trans_Package(int vType, byte[] data) {
        this.vType = vType;

        if (data.length % 4 == 0) {
            trans_package = new byte[16 + data.length];
        } else {
            // C 语言数据结构补位操作
            trans_package = new byte[16 + (data.length + (4 - data.length % 4))];
        }

        if (vType == vType_Audio) {
            setPackageId(getAudioPackageId());
        } else if (vType == vType_Video) {
            setPackageId(getVideoPackageId());
        } else {
            System.out.println("异常属性");
        }
        setvType(vType);
        setLen(trans_package.length);
        setData(data);
    }

    /**
     * 全 数组内容 初始化
     */
    public Trans_Package(byte[] bytes) {
        this.trans_package = bytes;
    }

    public int getvType() {
        return CommonMethods.bytesToInt(trans_package, 0);
    }

    public void setvType(int vType) {
        CommonMethods.intInsertIntoBytes(trans_package, 0, vType);
    }

    public int getPackageId() {
        return CommonMethods.bytesToInt(trans_package, 4);
    }

    public void setPackageId(int packageId) {
        CommonMethods.intInsertIntoBytes(trans_package, 4, packageId);
    }

    public int getLen() {
        return CommonMethods.bytesToInt(trans_package, 12);
    }

    public void setLen(int len) {
        CommonMethods.intInsertIntoBytes(trans_package, 12, len);
    }

    public void setData(byte[] data) {
        if (data == null) {
            setLen(0);
        } else {
            setLen(data.length);
            System.arraycopy(data, 0, trans_package, 16, data.length);
        }
    }

    public byte[] generateToBytes() {
        return trans_package;
    }

}
